package com.webside.Advertisingstatistics.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.webside.Advertisingstatistics.model.AdDailyStatEntity;
import com.webside.Advertisingstatistics.model.AdProvinceStatEntity;
import com.webside.Advertisingstatistics.model.AdSingleStatEntity;
import com.webside.Advertisingstatistics.model.AdbrowerRecordsEntity;
import com.webside.Advertisingstatistics.model.BaDvertisementEntity;
import com.webside.Advertisingstatistics.service.AdDailyStatService;
import com.webside.Advertisingstatistics.service.AdProvinceStatService;
import com.webside.Advertisingstatistics.service.AdSingLeStatService;
import com.webside.Advertisingstatistics.service.AdbrowerRecordsService;
import com.webside.Advertisingstatistics.service.BaDvertisementService;
import com.webside.playbackstatistics.model.AnaDailyStatEntity;
import com.webside.playbackstatistics.model.AnaSummaryStatEntity;
import com.webside.util.Common;

/***
 *
 * @author Administrator lpz广告统计
 */
@Controller
@Scope("prototype")
@RequestMapping("/adstat/")
public class AdbrowerRecordsController {
	@Autowired
	private AdDailyStatService adDailyStatService;
	@Autowired
	private AdbrowerRecordsService adbrowerRecordsService;
	@Autowired
	private AdSingLeStatService adSingLeStatService;
	@Autowired
	private BaDvertisementService baDvertisementService;
	@Autowired
	private AdProvinceStatService adProvinceStatService;
	@RequestMapping("listUI.html")
	public String selectAllPlayStat(Model model, HttpServletRequest request) {
		// 统计汇总默认显示全部
		List<AdDailyStatEntity> adDailyStatList = new ArrayList<AdDailyStatEntity>();

		adDailyStatList = adbrowerRecordsService.adDailyStatListAll();
		adbrowerRecordsService.AdSingleStatList();
		// adSingLeStatService.addAdSingleStat();
		// adSingLeStatService.deleteAdSingleStat();
		model.addAttribute("adDailyStatList", adDailyStatList);
		// 折线图默认显示今天的全部
		Map map = adbrowerRecordsService.advertisingFlow(null);
		model.addAttribute("todayData", map.get("todayData")); // 当前时间的数据

		// 广告位统计默认统计今日
		List<AdbrowerRecordsEntity> adbrowerRecordsList = new ArrayList<AdbrowerRecordsEntity>();
		adbrowerRecordsList = adbrowerRecordsService.adpositionListAll("j");
		model.addAttribute("adbrowerRecordsList", adbrowerRecordsList); // 当前时间的数据
		
		//广告投放区域统计默认查询全部 饼形图默认查询全部广告
		List<AdProvinceStatEntity> adProvinceStatCkList = new ArrayList<AdProvinceStatEntity>();
		adProvinceStatCkList = adProvinceStatService.adProvinceStatCkList(null);
		model.addAttribute("adProvinceStatCkList",adProvinceStatCkList); // 全部
		
		return Common.BACKGROUND_PATH + "/adbrowerRecordsStat/list";
	}

	// 根据广告名统计该广告的信息
	@RequestMapping("AdTitlelistUI.html")
	@ResponseBody
	public Object selectAdTitle(Model model, HttpServletRequest request, String adTitle) {
		Map map = new HashMap();
		if (null != adTitle && !adTitle.trim().equals("")) {
			int index = 0;
			List<BaDvertisementEntity> baDvertisement = baDvertisementService.selectBaDvertisementAll();
			for (BaDvertisementEntity bde : baDvertisement) {
				if (adTitle.trim().equals(bde.getTitle())) {

					index = 1;
				}

			}

			// 获取改广告今日的统计 信息
			AdbrowerRecordsEntity adbrowerRecords = new AdbrowerRecordsEntity();
			// 获取该广告的统计信息
			AdSingleStatEntity adSingleStat = new AdSingleStatEntity();
			if (index == 1) {
				adbrowerRecords = adbrowerRecordsService.selectSumCollectStat(adTitle, null);
				adSingleStat = adSingLeStatService.selectAdSingleStat(adTitle.trim());
			}

			// 当前广告的 今日 数据
			map.put("adbrowerRecords", adbrowerRecords);
			// 当前广告的昨日平均 峰值 累计 数据
			map.put("adSingleStat", adSingleStat);
		} else {
			// 统计汇总默认显示全部
			List<AdDailyStatEntity> adDailyStatList = new ArrayList<AdDailyStatEntity>();

			adDailyStatList = adbrowerRecordsService.adDailyStatListAll();

			map.put("adDailyStatList", adDailyStatList);
		}
		return map;

	}

	// 根据广告名称统计该广告的今天和明天趋势图
	@RequestMapping("zheXianlistUI.html")
	@ResponseBody
	public Object selectAdTitleZX(Model model, HttpServletRequest request, String title) {
		Map map = new HashMap();
		map = adbrowerRecordsService.advertisingFlow(title.trim());
		return map;
	}

	// 根据时间区域来进行广告位统计
	@RequestMapping("advertisinglistUI.html")
	@ResponseBody
	public Object selectAdTitlePeriod(Model model, HttpServletRequest request, String period) {
		Map map = new HashMap();
		List<AdbrowerRecordsEntity> adbrowerRecordsList = new ArrayList<AdbrowerRecordsEntity>();
		adbrowerRecordsList = adbrowerRecordsService.adpositionListAll(period.trim());

		// 将传来的数据处理
		List tempList = Arrays.asList(adbrowerRecordsList.toArray());
		// 新的数据
		adbrowerRecordsList = new ArrayList<AdbrowerRecordsEntity>(tempList);
		map.put("adbrowerRecordsList", adbrowerRecordsList);
		return map;
	}
	
	// 根据广告名称统计该广告的今天和明天趋势图
	@RequestMapping("binXinglistUI.html")
	@ResponseBody
	public Object selectAdTitleBX(Model model, HttpServletRequest request, String title) {
		Map map = new HashMap();
		List<AdProvinceStatEntity> adProvinceStatCkList = new ArrayList<AdProvinceStatEntity>();
		adProvinceStatCkList = adProvinceStatService.adProvinceStatCkList(title.trim());
		map.put("adProvinceStatCkList",adProvinceStatCkList);
		return map;
	}


}
